package javaapplication3;

import java.io.IOException;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Matt
 */
public class Options extends javax.swing.JFrame {

    /**
     * Creates new form Options
     */
    DefaultListModel allClassListModel = new DefaultListModel();
    DefaultListModel currentClassListModel = new DefaultListModel();
    InstanceCall Inst;
    public void  OptionsStart() {
        Inst = new InstanceCall();
        initComponents();
        addNewClass.setVisible(true);
        classNameL.setVisible(false);
        classNumberL.setVisible(false);
        sectionNumberL.setVisible(false);
        className.setVisible(false);
        classNumber.setVisible(false);
        sectionNumber.setVisible(false);
        cancelButton.setVisible(false);
        addNewButton.setVisible(false);
        //all list model loads item from database
        //current list model loads item from current classes folder
        allClassList.setModel(allClassListModel);
        currentClassList.setModel(currentClassListModel);
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        addNewClass = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        allClassList = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        currentClassList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        addArrow = new javax.swing.JButton();
        removeArrow = new javax.swing.JButton();
        className = new javax.swing.JTextField();
        classNumber = new javax.swing.JTextField();
        sectionNumber = new javax.swing.JTextField();
        classNameL = new javax.swing.JLabel();
        classNumberL = new javax.swing.JLabel();
        sectionNumberL = new javax.swing.JLabel();
        addNewButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        EditMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList1);

        jTextArea1.setEditable(false);
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText("Art 101-001\nArt 201-002\nArt 401-004\nArt 501-005\nArt 601-006\nArt 701-007\nArt 801-009");
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(530, 475));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Add/Remove Classes");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 21, -1, -1));
        getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 45, 470, 10));

        addNewClass.setText("Add New Class");
        addNewClass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewClassActionPerformed(evt);
            }
        });
        getContentPane().add(addNewClass, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 290, -1, -1));

        jButton4.setText("Close");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 382, -1, -1));

        jScrollPane3.setViewportView(allClassList);

        getContentPane().add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, 182, 189));

        jScrollPane4.setViewportView(currentClassList);

        getContentPane().add(jScrollPane4, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 90, 185, 190));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Current Classes");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 70, 129, 19));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("All Classes");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 81, 19));

        addArrow.setText("----->");
        addArrow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addArrowActionPerformed(evt);
            }
        });
        getContentPane().add(addArrow, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 140, -1, -1));

        removeArrow.setText("<-----");
        removeArrow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeArrowActionPerformed(evt);
            }
        });
        getContentPane().add(removeArrow, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 180, -1, -1));
        getContentPane().add(className, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 290, 124, -1));
        getContentPane().add(classNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 320, 124, -1));
        getContentPane().add(sectionNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 350, 124, -1));

        classNameL.setText("Class Name:");
        getContentPane().add(classNameL, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 290, -1, -1));

        classNumberL.setText("Class Number:");
        getContentPane().add(classNumberL, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 320, -1, -1));

        sectionNumberL.setText("Section Number:");
        getContentPane().add(sectionNumberL, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 350, -1, -1));

        addNewButton.setText("Add");
        addNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewButtonActionPerformed(evt);
            }
        });
        getContentPane().add(addNewButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 380, -1, -1));

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        getContentPane().add(cancelButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 380, -1, -1));

        EditMenu.setText("Help");

        jMenuItem1.setText("Contents");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        EditMenu.add(jMenuItem1);

        jMenuBar1.add(EditMenu);

        setJMenuBar(jMenuBar1);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void addNewClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewClassActionPerformed
        // TODO add your handling code here:
        addNewClass.setVisible(false);
        classNameL.setVisible(true);
        classNumberL.setVisible(true);
        sectionNumberL.setVisible(true);
        className.setVisible(true);
        classNumber.setVisible(true);
        sectionNumber.setVisible(true);
        cancelButton.setVisible(true);
        addNewButton.setVisible(true);
        //saves added class to the database in future

    }//GEN-LAST:event_addNewClassActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void addArrowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addArrowActionPerformed
        // TODO add your handling code here:
        int i;
        boolean found = false;
        for (i = 0; i < currentClassListModel.getSize(); i++) {
            if (currentClassListModel.elementAt(i).equals(allClassList.getSelectedValue())) {
                found = true;
            }
        }
        if (found) {
            JOptionPane.showMessageDialog(null, "Class already in current class list", "Add Error", JOptionPane.ERROR_MESSAGE);
        } else {
            currentClassListModel.addElement(allClassList.getSelectedValue());
        }

    }//GEN-LAST:event_addArrowActionPerformed

    private void removeArrowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeArrowActionPerformed
        // TODO add your handling code here:
        int i;
        for (i = 0; i < currentClassListModel.getSize(); i++) {
            if (currentClassListModel.elementAt(i).equals(currentClassList.getSelectedValue())) {
                currentClassListModel.removeElementAt(i);
            }
        }

    }//GEN-LAST:event_removeArrowActionPerformed

    private void addNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewButtonActionPerformed
        if (className.getText().equals("") | classNumber.getText().equals("") | sectionNumber.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Please enter all three values", "Add Error", JOptionPane.ERROR_MESSAGE);
        } else {
            //allClassListModel.addElement(className.getText() + " " + classNumber.getText() + "." + sectionNumber.getText());
            addNewClass.setVisible(true);
            classNameL.setVisible(false);
            classNumberL.setVisible(false);
            sectionNumberL.setVisible(false);
            className.setVisible(false);
            classNumber.setVisible(false);
            sectionNumber.setVisible(false);
            cancelButton.setVisible(false);
            addNewButton.setVisible(false);
        
        int i;
        boolean found = false;
        String input = className.getText() + " " + classNumber.getText() + "." + sectionNumber.getText();
        input = input.toLowerCase(); 
        for (i = 0; i < allClassListModel.getSize(); i++) {
            if (allClassListModel.elementAt(i).toString().toLowerCase().equals(input)) {
                found = true;
            }
        }
        if (found) {
            JOptionPane.showMessageDialog(null, "Class already in all class list", "Add Error", JOptionPane.ERROR_MESSAGE);
        } else {
            allClassListModel.addElement(input.toUpperCase());
        } }

    }//GEN-LAST:event_addNewButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        addNewClass.setVisible(true);
        classNameL.setVisible(false);
        classNumberL.setVisible(false);
        sectionNumberL.setVisible(false);
        className.setVisible(false);
        classNumber.setVisible(false);
        sectionNumber.setVisible(false);
        cancelButton.setVisible(false);
        addNewButton.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
         try {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + Inst.getPDF());
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Error");  //print the error
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu EditMenu;
    private javax.swing.JButton addArrow;
    private javax.swing.JButton addNewButton;
    private javax.swing.JButton addNewClass;
    private javax.swing.JList allClassList;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField className;
    private javax.swing.JLabel classNameL;
    private javax.swing.JTextField classNumber;
    private javax.swing.JLabel classNumberL;
    private javax.swing.JList currentClassList;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton removeArrow;
    private javax.swing.JTextField sectionNumber;
    private javax.swing.JLabel sectionNumberL;
    // End of variables declaration//GEN-END:variables
}
